/**
 * 类装饰器:ClassDecorator
 * 属性装饰器:PropertyDecorator
 * 参数装饰器:ParameterDecorator
 * 方法装饰器:MethodDecorator
 * 装饰器工厂:
 * import 'reflect-metadata'(源数据的反射)
 * axios
 */
import axios from 'axios'
import 'reflect-metadata'
/**
 *类装饰器
 * @param target:构造函数
 * @constructor
 */
const Base:ClassDecorator = (target) => {
    // console.log(target)
    target.prototype.xiaoman = 'ts'
    target.prototype.fn = () => {
        // console.log('我是憨憨')
    }
}
@Base
class Http {
//...
}
const http = new Http() as any
http.fn()

/**
 * 装饰器工厂(采用函数柯里化接收外部装饰器床过来的参数)
 */
const Base1 = (value:string) => {
    const fn:ClassDecorator = (target)  => {
        // console.log(target)
        target.prototype.xiaoman = value
        target.prototype.fn = () => {
            console.log('我是憨憨')
        }
    }
    return fn
}
@Base1('xiaozhou')
class Http1 {
    //....
}
const http1 = new Http1()
// console.log(http1)

//自己实现
const post= (url:string) => {
    const fn:MethodDecorator = () => {

    }
    return fn
}
const Get = (url:string) => {
    const fn:MethodDecorator = (target, propertyKey, descriptor:PropertyDescriptor) => {
        // console.log(target,propertyKey,descriptor);
        const key = Reflect.getMetadata('key',target)
        axios.get(url).then( res => {
            descriptor.value(key ? res.data[key] : res.data)
        })
    }
    return fn
}
const Result = () => {
    const fn:ParameterDecorator = (target, propertyKey, parameterIndex) => {
        // console.log(target,propertyKey,parameterIndex);
        /**
         * metadataKey:存储的时候的key
         * metadataKey:对应返回结果的字段result
         * target:回传的对象
         */
        Reflect.defineMetadata('key','result',target)
    }
    return fn
}
const Name = (str:string) => {
    const fn:PropertyDecorator = (target,key) => {
        console.log(target,key);
    }
    return fn
}
@Base1('xiaoyu')
class Http2 {
    @Name('')
    name:string
    constructor(name: string) {
        this.name = name
    }
    @Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10')
    getList (@Result() data:any) {
        //不加Result的参数装饰器需要点result
        // console.log(data.result.list)
        //使用Result装饰器的返回的结果不需要点result点list
        console.log(data)
    }
    @post('')
    create () {

    }
}
const http2 = new Http2('')
